package repository

import (
	"aigen-todo/internal/interfaces"
	"aigen-todo/internal/models"

	"gorm.io/gorm"
)

type TaskGroupRepository struct {
	db *gorm.DB
}

// 确保 TaskGroupRepository 实现了 interfaces.TaskGroupRepositoryInterface 接口
var _ interfaces.TaskGroupRepositoryInterface = &TaskGroupRepository{}

func NewTaskGroupRepository(db *gorm.DB) *TaskGroupRepository {
	return &TaskGroupRepository{db: db}
}

func (r *TaskGroupRepository) Create(group *models.TaskGroup) error {
	return r.db.Create(group).Error
}

func (r *TaskGroupRepository) GetByID(id uint64) (*models.TaskGroup, error) {
	var group models.TaskGroup
	err := r.db.First(&group, id).Error
	return &group, err
}

func (r *TaskGroupRepository) GetByUserID(userID uint64) ([]models.TaskGroup, error) {
	var groups []models.TaskGroup
	err := r.db.Where("user_id = ?", userID).Find(&groups).Error
	return groups, err
}

func (r *TaskGroupRepository) GetDefaultGroupByUserID(userID uint64) (*models.TaskGroup, error) {
	var group models.TaskGroup
	err := r.db.Where("user_id = ? AND is_default = ?", userID, true).First(&group).Error
	return &group, err
}

func (r *TaskGroupRepository) Update(group *models.TaskGroup) error {
	return r.db.Save(group).Error
}

func (r *TaskGroupRepository) Delete(id uint64) error {
	return r.db.Delete(&models.TaskGroup{}, id).Error
}
